package org.zk.controller;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Admin;
import org.zk.service.AdminService;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 说明:管理员业务相关的控制器
 *
 * @姚文斌
 * @create 2019-11-18 上午 11:37
 */
@RestController
public class AdminController {
    //注入service层
    @Autowired
    @Setter
    @Getter
    private AdminService adminService;

    /**
     * ================================== admin各个业务的控制器 ==================================
     */
//    //1.同步的登录
//    @RequestMapping("/adminLogin")
//    public String adminLogin(Admin a, HttpSession session){
//        Admin admin=adminService.adminLogin(new Admin(a.getAdminid(),a.getAdminpassword()));
//        if (admin!=null){
//            session.setAttribute("admin",admin);//把admin对象存到session域
//            return "redirect:toIndex.do";
//        }else {
//            return "redirect:toLogin.do";
//        }
//
//    }
//    //2.通过登录验证后转到跳转index.ftl的控制器
//    @RequestMapping("/toIndex")
//    public ModelAndView toIndex() {
//        return new ModelAndView("index");
//    }//通过控制器转到对应的freeMarker页面-index.ftl
//
//    //3.未通过登录验证后转到跳转index.ftl的控制器
//    @RequestMapping("/toLogin")
//    public ModelAndView toLogin() {
//        return new ModelAndView("login");
//    }//通过控制器转到对应的html页面-login.html

    //1.异步的登录
    @RequestMapping("/adminLogin")
    public Map adminLogin(Admin a, HttpSession session){
        Admin admin=adminService.adminLogin(new Admin(a.getAdminid(),a.getAdminpassword()));
        Map map=new HashMap();
        if (admin!=null){
            session.setAttribute("admin",admin);//把admin对象存到session域
            map.put("admin",admin);
            return map;
        }else {
            map.put("admin","fail");
            return map;//把map返回给异步函数的回调函数
        }

    }
    //2.通过登录验证后转到跳转index.ftl的控制器
    @RequestMapping("/toIndex")
    public ModelAndView toIndex() {
        return new ModelAndView("index");
    }//通过控制器转到对应的freeMarker页面-index.ftl





}
